/******************************************************************************
@file:  GymRegistration.java
@author Ramesh Anandhan

DESCRIPTION
-----------------------------------------------------------------------------
Copyright (c) 2011, GEEKWAVE SOLUTIONS INC.

All rights reserved.
******************************************************************************/
package com.smartc.health;

import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.smartc.connection.HttpConnection;
//import com.smartc.connection.ParsedLoginData;

import com.smartc.ui.AccountLogin;
import com.smartc.ui.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class GymRegistration extends Activity implements OnItemClickListener {
	protected static final String TAG = "GymRegistration";
	private static final int DIALOG_REGISTER = 0x01;
	private static final int REG_STATUS = 0x02;
	private ArrayList<Object> itemList;
	private ItemBean bean;

	ListView mItemListView;
	ListViewAdapter adapter;

	private HttpConnection mConnection;
	private ResponseHandler mResponse = new ResponseHandler();
	private int mSelectedItem = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.yoga_class);
		mConnection = new HttpConnection();
		mConnection.AddHandler(mResponse);

		TextView header = (TextView) findViewById(R.id.header_text);
		header.setText(getResources().getString(R.string.health_upcoming_gym));
	}

	@Override
	protected void onStart() {
		super.onStart();
		String domain = getResources().getString(R.string.domain);
		String query = getResources().getString(
				R.string.url_health_care_gym_yoga);
		String url = domain + query;
		if (AccountLogin.DEBUG)
			Log.i(TAG, "onStart url:" + url);

		mConnection.setUrl(url);
		mConnection.clearParam();
		mConnection.AddParam("idType", "1");
		mConnection.Execute();
	}

	public static class ViewHolder {
		ImageView ItemImage;
		TextView ItemSeatCount;
		TextView ItemsTime;
		TextView ItemPrice;
		TextView ItemName;
	}

	private class ListViewAdapter extends BaseAdapter {

		private LayoutInflater inflater;
		// private Context mContext;
		ArrayList<Object> mItemList;

		ListViewAdapter(Context context, ArrayList<Object> itemList) {
			super();
			// mContext = context;
			mItemList = itemList;
			this.inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return mItemList.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.yoga_class_list, null);

				holder.ItemImage = (ImageView) convertView
						.findViewById(R.id.yoga_image);
				holder.ItemsTime = (TextView) convertView
						.findViewById(R.id.yoga_class_time);
				holder.ItemSeatCount = (TextView) convertView
						.findViewById(R.id.yoga_class_seats);
				holder.ItemPrice = (TextView) convertView
						.findViewById(R.id.yoga_class_price);
				holder.ItemName = (TextView) convertView
						.findViewById(R.id.yoga_class_name);

				convertView.setTag(holder);
			} else
				holder = (ViewHolder) convertView.getTag();

			ItemBean bean = (ItemBean) itemList.get(position);

			holder.ItemImage.setImageResource(bean.getImage());
			holder.ItemPrice.setText(bean.getItemPrice());
			holder.ItemsTime.setText(bean.getItemTime());
			holder.ItemName.setText(bean.getItemName());
			holder.ItemSeatCount.setText(bean.getItemSeatCount());
			return convertView;
		}
	}

	public class ItemBean {
		String mItemSeatCount;
		String mItemTime;
		String mItemName;
		String mItemPrice;
		int image;

		public String getItemSeatCount() {
			return mItemSeatCount;
		}

		public void setItemSeatCount(String seat) {
			this.mItemSeatCount = seat;
		}

		public String getItemName() {
			return mItemName;
		}

		public void setItemName(String name) {
			this.mItemName = name;
		}

		public String getItemPrice() {
			return mItemPrice;
		}

		public void setItemPrice(String price) {
			this.mItemPrice = price;
		}

		public String getItemTime() {
			return mItemTime;
		}

		public void setItemTime(String itemtime) {
			this.mItemTime = itemtime;
		}

		public int getImage() {
			return image;
		}

		public void setImage(int image) {
			this.image = image;
		}

	}

	public void AddObjectToList(int image, String seatcount, String price,
			String time, String name) {

		bean = new ItemBean();
		bean.setItemSeatCount(seatcount);
		bean.setItemPrice(price);
		bean.setItemTime(time);
		bean.setItemName(name);
		bean.setImage(image);
		itemList.add(bean);
	}

	public class ResponseHandler extends DefaultHandler {

		private boolean nrTotalSeat = false;
		private boolean name = false;
		private boolean idEmail = false;
		private boolean id = false;
		private boolean message = false;
		private boolean slotTime1 = false;
		private boolean slotTime2 = false;
		private boolean nrFee = false;

		public ArrayList<String> mSeat_List = new ArrayList<String>();
		public ArrayList<String> mName_List = new ArrayList<String>();
		public ArrayList<String> mEmail_List = new ArrayList<String>();
		public ArrayList<String> mId_List = new ArrayList<String>();
		public ArrayList<String> mPrice_List = new ArrayList<String>();
		public ArrayList<String> mslotTime1_List = new ArrayList<String>();
		public ArrayList<String> mslotTime2_List = new ArrayList<String>();
		public String mMessage = null;
		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (this.nrTotalSeat) {
				mSeat_List.add(new String(ch, start, length));
			}else if (this.name) {
				mName_List.add(new String(ch, start, length));
			}else if (this.idEmail) {
				mEmail_List.add(new String(ch, start, length));
			}else if (this.id) {
				mId_List.add(new String(ch, start, length));
			}else if (this.message) {
				mMessage = new String(ch, start, length);
			} else if (this.nrFee) {
				mPrice_List.add(new String(ch, start, length));
			} else if (this.slotTime1) {
				mslotTime1_List.add(new String(ch, start, length));
			} else if (this.slotTime2) {
				mslotTime2_List.add(new String(ch, start, length));
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if (localName.equals("Root")) {
				Message myMessage = new Message();
				myMessage.obj = "ROOT";
				handler.sendMessage(myMessage);
			}else if (localName.equals("id")) {
				this.id = false;
			}else if (localName.equals("nrTotalSeat")) {
				this.nrTotalSeat = false;
			}else if (localName.equals("name")) {
				this.name = false;
			}else if (localName.equals("idEmail")) {
				this.idEmail = false;
			}else if (localName.equals("message")) {
				this.message = false;
			}else if (localName.equals("Messages")) {
				Message myMessage = new Message();
				myMessage.obj = "MESSAGE";
				handler.sendMessage(myMessage);
			} else if (localName.equals("nrFee")) {
				this.nrFee = false;
			} else if (localName.equals("slotTime1")) {
				this.slotTime1 = false;
			} else if (localName.equals("slotTime2")) {
				this.slotTime2 = false;
			}
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if (localName.equals("Root")) {
				// this.TradeProducts = true;
				mSeat_List.clear();
				mName_List.clear();
				mEmail_List.clear();
				mId_List.clear();
				mslotTime1_List.clear();
				mslotTime2_List.clear();
				mPrice_List.clear();
			}else if (localName.equals("id")) {
				this.id = true;
			}else if (localName.equals("nrTotalSeat")) {
				this.nrTotalSeat = true;
			} else if (localName.equals("name")) {
				this.name = true;
			}else if (localName.equals("idEmail")) {
				this.idEmail = true;
			}else if (localName.equals("message")) {
				this.message = true;
			} else if (localName.equals("nrFee")) {
				this.nrFee = true;
			} else if (localName.equals("nrFee")) {
				this.nrFee = true;
			} else if (localName.equals("slotTime1")) {
				this.slotTime1 = true;
			} else if (localName.equals("slotTime2")) {
				this.slotTime2 = true;
			}
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage msg:" + (String) msg.obj);
			String loginmsg = (String) msg.obj;
			
			ArrayList<String> mTime_List = new ArrayList<String>();
			
			for(int i=0;i<mResponse.mName_List.size();i++){
				mTime_List.add(mResponse.mslotTime1_List.get(i)
						       +" and "+mResponse.mslotTime2_List.get(i));
			}
			
			if (loginmsg != null && loginmsg.equals("ROOT")) {
				itemList = new ArrayList<Object>();
				for (int i = 0; i < mResponse.mName_List.size(); i++) {
					AddObjectToList(R.drawable.yoga_icon, "Seat Count: "
							+ mResponse.mSeat_List.get(i), "Price: "
							+ mResponse.mPrice_List.get(i), "Time: "
							+ mTime_List.get(i), mResponse.mName_List
							.get(i));
				}

				mItemListView = (ListView) findViewById(R.id.yoga_class_list);
				adapter = new ListViewAdapter(GymRegistration.this
						.getApplicationContext(), itemList);
				mItemListView.setAdapter(adapter);
				mItemListView.setOnItemClickListener(GymRegistration.this);
			}else if(loginmsg != null && loginmsg.equals("MESSAGE")){
				showDialog(REG_STATUS);
			}
		}
	};

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		if (AccountLogin.DEBUG)
			Log.i(TAG, "onItemClick position" + position);
		mSelectedItem = position;
		//showDialog(DIALOG_REGISTER);
		Intent intent = new Intent(GymRegistration.this,GymYogaRegistration.class);
		intent.putExtra("idType", "1");
		intent.putExtra("id", mResponse.mId_List.get(position));
		intent.putExtra("nrTotalSeatCapacity", mResponse.mSeat_List.get(position));
		startActivity(intent);
	}

	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		if (AccountLogin.DEBUG)
			Log.i(TAG, "onCreateDialog id" + id);
		switch (id) {
		case DIALOG_REGISTER:
			LayoutInflater factory = LayoutInflater.from(this);
			final View textEntryView = factory.inflate(R.layout.gym_yoga_reg,
					null);
			TextView seatCount = (TextView) textEntryView
					.findViewById(R.id.gym_yoga_seatcount);
			TextView Price = (TextView) textEntryView
					.findViewById(R.id.gym_yoga_price);
			TextView Name = (TextView) textEntryView
					.findViewById(R.id.gym_yoga_name);
			TextView Time = (TextView) textEntryView
					.findViewById(R.id.gym_yoga_time);
			seatCount.setText("Seat Count: "
					+ mResponse.mSeat_List.get(mSelectedItem));
			Price.setText("Price: " + "1000");
			Name.setText("Name: " + mResponse.mName_List.get(mSelectedItem));
			Time.setText("Time: " + "2011-10-11 00:00");
			return new AlertDialog.Builder(GymRegistration.this).setTitle(
					R.string.health_doc_register).setView(textEntryView)
					.setPositiveButton(R.string.confirm,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
/*									String domain = getResources().getString(
											R.string.domain);
									String query = getResources().getString(
											R.string.url_health_care_gym_yoga_registration);
									String url = domain + query;
									if (AccountLogin.DEBUG)
										Log.i(TAG, "onStart url:" + url);

									mConnection.setUrl(url);
									mConnection.clearParam();
									mConnection.AddParam("idType", "1");
									mConnection.AddParam("id", mResponse.mId_List.get(mSelectedItem));
									mConnection.AddParam("idUser", ParsedLoginData.mIdUser);
									mConnection.AddParam("nrTotalSeatCapacity", mResponse.mSeat_List.get(mSelectedItem));
									mConnection.AddParam("idSlot", "M");
									mConnection.AddParam("idAssociate", "A12765");
									mConnection.AddParam("nmName", "ramesh");
									mConnection.AddParam("nmLocation", "TNagar");
									mConnection.AddParam("inGender", "M");
									mConnection.Execute();*/
									//removeDialog(DIALOG_REGISTER);
								}

							}).setNegativeButton(R.string.cancel,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									//removeDialog(DIALOG_REGISTER);
								}
							}).create();
		case REG_STATUS:
			return new AlertDialog.Builder(GymRegistration.this).setMessage(
					mResponse.mMessage).setPositiveButton("OK",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							removeDialog(REG_STATUS);
						}
					}).create();
		default:
			return super.onCreateDialog(id, args);
		}
	}
}
